﻿using stdCall.History;
using stdCall.Models;
using stdCall.Resource;
using System;
using System.Collections;
using System.Windows.Forms;

namespace stdCall.MyEvaluation
{
    /// <summary>
    /// 评价类
    /// </summary>
    class Evaluation
    {

        public static ArrayList picStars;
        public static int historyPosx = 0;
        public static int historyPosy = 0;
        public static Label latestHistoryLabel = new Label();
        private static bool firstFlag = true;

        private const int blankInside = 5;
        private const int blankOutside = 10;
        private const int picStarHeight = 20;
        /// <summary>
        /// 待优化，评分，并生成星级图片到历史记录栏，直接添加至外部容器，并返回一个包含学生姓名的历史记录Label，在外部添加
        /// </summary>
        /// <param name="picstar">星级评价（0-5）</param>
        /// <param name="currentStd"></param>
        /// <param name="collection">容器类</param>
        /// <returns>包含学生姓名的历史记录Label</returns>
        public void markStarAndAddHisLabel(PictureBox picstar, Student currentStd,Control.ControlCollection collection)
        {
            
            if (currentStd == null) return ;
            int stars = 0;
            foreach (PictureBox pb in picStars)
            {
                if (pb != picstar)
                {
                    pb.Load("star_yellow.png");
                    stars++;
                }
                else
                {
                    pb.Load("star_yellow.png");
                    stars++;
                    break;
                }
            }

            new StudentsManager().getStudentByNum(currentStd.Number).UnluckyCallNum++;
            new CurrentHistory().addUnluckyCall(currentStd.Number,stars,ResourseLoader.CourseID);
            if (firstFlag)
            {
                latestHistoryLabel = generateHistoryLabelStars(collection, currentStd.Name, stars, historyPosx, historyPosy);
                firstFlag = false;
            }
            else
            {
                latestHistoryLabel = generateHistoryLabelStars(collection, currentStd.Name, stars, latestHistoryLabel);
            }
            collection.Add(latestHistoryLabel);
        }


        private Label generateHistoryLabelStars(Control.ControlCollection collection, String text, int stars, Label label)
        {
      
            generateHistoryStar(collection, stars, label.Location.X, label.Location.Y + label.Size.Height * 2 + picStarHeight + blankInside*2 + blankOutside);
            return generateLabel(text, label.Location.X, label.Location.Y + label.Size.Height + blankOutside + picStarHeight+blankInside);
        }

        /// <summary>
        /// 生成一个历史记录Label
        /// </summary>
        /// <param name="text">Label内容</param>
        /// <param name="posx">Label位置x</param>
        /// <param name="posy">Label位置y</param>
        /// <returns></returns>
        public Label generateHistoryLabel(String text, int posx, int posy)
        {
            historyPosy += 45;
            return generateLabel(text,posx,posy);
        }

        /// <summary>
        /// 生成星级图片到历史记录栏，直接添加至外部容器，并返回一个包含学生姓名的历史记录Label，在外部添加
        /// </summary>
        /// <param name="collection">外部容器</param>
        /// <param name="text"></param>
        /// <param name="stars">星级（0-5）</param>
        /// <param name="posx"></param>
        /// <param name="posy"></param>
        /// <returns></returns>
        public Label generateHistoryLabelStars(Control.ControlCollection collection,String text, int stars,int posx, int posy)
        {
           /* historyPosy += 65;*/
            Label lable=generateLabel(text, posx, posy);
            generateHistoryStar(collection, stars,posx, posy +lable.Size.Height+blankInside);
            return lable;
        }

        /// <summary>
        /// 生成并返回一个Label
        /// </summary>
        /// <param name="text"></param>
        /// <param name="posx"></param>
        /// <param name="posy"></param>
        /// <returns></returns>
        private Label generateLabel(String text, int posx, int posy)
        {
            Label mylabel = new Label();
            mylabel.AutoSize = true;
            mylabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            mylabel.Font = new System.Drawing.Font("Buxton Sketch", 21.75F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            mylabel.Location = new System.Drawing.Point(posx, posy);
            mylabel.Size = new System.Drawing.Size(164, 38);
            mylabel.TabIndex = 1;
            mylabel.Text = text;
            return mylabel;
        }

        /// <summary>
        /// 根据评分生成历史记录栏的星级，直接添加至外部容器
        /// </summary>
        /// <param name="collection">外部容器</param>
        /// <param name="stars"></param>
        /// <param name="posx"></param>
        /// <param name="posy"></param>
        public void generateHistoryStar(Control.ControlCollection collection,int stars,int posx,int posy){
            int starPosX = posx;
            int starPosY = posy;
            for (int i = 0; i < stars; i++)
            {

                System.Windows.Forms.PictureBox pictureBox_star;
                pictureBox_star = new System.Windows.Forms.PictureBox();
                pictureBox_star.Location = new System.Drawing.Point(starPosX, starPosY);
                pictureBox_star.Name = "pictureBox_stars";
                pictureBox_star.Size = new System.Drawing.Size(picStarHeight, picStarHeight);
                pictureBox_star.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                pictureBox_star.TabIndex = 3;
                pictureBox_star.TabStop = false;
               /* pictureBox_star.Click += new System.EventHandler(this.pictureBox_stars_Click);*/
                pictureBox_star.Load("star_yellow.png");
                collection.Add(pictureBox_star);
               // picStars.Add(pictureBox_star);
                starPosX += 25;
            }
        }
    }
}
